package actions;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import db.DbConnectionPool;
import db.DbException;

public class reviewerAction extends Action{
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		
		forms.ReviewHForm rf = (forms.ReviewHForm) form;
		
		Connection conn = null;
		Statement stmt = null;
		Statement stmt2 = null;
		
		ResultSet rs = null;
		ResultSet rs2 = null;
		String instruc = null;
		
		// Registering Postgresql JDBC driver
        try {
        	
	        // Open a connection to the database    
        	
			conn = DbConnectionPool.getConnection();
			stmt = conn.createStatement();
			stmt2 = conn.createStatement();
				        	        
	        rs = stmt.executeQuery("SELECT user_id FROM users WHERE user_name ='" +
	        		request.getUserPrincipal().getName() + "'");
	        rs.next();
	        int userID = rs.getInt("user_id");
	        
	        rs = stmt.executeQuery("SELECT * FROM reviewer_assignments WHERE reviewer ='" +
	        		userID + "'");
	        
	        while(rs.next()){
	        	int app_id = rs.getInt("application");
	        		        	
	        		rs2 = stmt2.executeQuery("SELECT * FROM applicants WHERE id ='"
	        								+app_id+"'");
	        			   
	        		while(rs2.next()) {
	        				        			
	        			rf.updateNames(new forms.nameform(rs2.getString("first_name"),
	        						rs2.getString("middle_name"),rs2.getString("last_name"),
	        						request.getUserPrincipal().getName()));
	        			
	        		}
	        		
	        	}	        
	    
	        
        } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException ex) {
		}

		
		return mapping.findForward("start");
	}
}